$(function() {
	laydate.render({
	    elem: '#year',
	    trigger: 'click',
	    type: 'year',
	    done: function(value, date) {
	        $('#year').val(value);
	    }
	})

	laydate.render({
	    elem: '#submit_year',
	    trigger: 'click',
	    type: 'year',
	    done: function(value, date) {
	        $('#submit_year').val(value);
	    }
	})

  $('#name').searchableSelect();

	$('#search').click(function(event) {
		search();
	});

	$("#reset").click(function(event) {
		$("#name").val('-1');
		$(".searchable-select .searchable-select-holder").html('请选择成果人');
		$("#year").val('');
		$("#submit_year").val('');
		$("#type").val('-1');
		$("#information").hide();
		$("#result").html('');
	});

	$("#dept").change(function(event) {
		search();
	});
	$("#name").change(function(event) {
		search();
	});
	$("#type").change(function(event) {
		search();
	});

});
function search(){

	var name=$("#name").val();
	var dept=$("#dept").val();
	var year=$("#year").val();
	var submit_year=$("#submit_year").val();
	var type=$("#type").val();
	var search_type=$('input[name="search_type"]:checked').val();
	$.post(getRootPath()+"/Admin/Results/schoolData", 
		{
			name:name,
			dept:dept,
			year:year,
			submit_year:submit_year,
			type:type,
			search_type:search_type
		}, 
		function(data) {
		if(data.status){
			var t=JSON.parse(data.v);
			if(search_type==1){				
				// 搜索结果
				$("#result").html('');
				$('.tu').hide();
				html1='';
				//console.log(t)
				$.each(t, function(k,v) {
					if(v.data.length!=0){
						// html1+='<div class="alert alert-info"><h1>'+v.dept+'</h1></div>';
						 var html2='<div class="dept_name"><div class="hr hr-18 dotted hr-double"></div><h4 class="pink"> <i class="fa fa-flag blue bigger-130"></i> <a href="#modal-table" role="button" class="green" data-toggle="modal">'+v.dept+'</a> </h4><div class="hr hr-18 dotted hr-double"></div></div>' ;
						$.each(v.data, function(k1, v1) {
							if(v1.data.length!=0){
								html2+='<div class="col-xs-12"> <div class="page-header"> <h3>'+v1.table_name+'</h3> </div><table  class="table  table-bordered table-hover "><thead> <tr>';
								if(v1.data.length!=0){
								 	$.each(v1.table_col, function(k1, v1) {
										html2+="<th style=\"width:350px;\"><div>"+v1+"</div></th>";
								 	});
								 	html2+=" </tr> </thead><tbody>";
								 	$.each(v1.data, function(k2, v2) {
								 		html2+="<tr>";
								 		$.each(v2, function(k3, v3) {
								 			if(k3=='id'){
												html2+="<td class='center'> <label class='pos-rel'> <input type='checkbox' class='ace' name='check["+k1+"][]' value='"+v3+"' checked> <span class='lbl'></span> </label> </td><td>"+(k2+1)+"</td>";
											 }else{
												if(k3 == 'only_code'){
													html2+="<td><div style=\"word-wrap: break-word;width:100px;\">"+v3+"</div></td>";
												}else{
													html2+="<td><div>"+v3+"</div></td>";
												}
											 }
								 		});
								 		html2+="<tr>";
								 	});
								 }
								 html2+="</tbody> </table></div>";
								 html1+=html2;
								html2='';
							}else{
								html2 +='';
							}						
						});
					}				
				});
				$("#result").append(html1);	
			}else{
				$('.tu').show();
				$("#result").html('');
				var html=$(".test");
				$.each(html, function(k, v) {
					$(v).html('')
				});
				$.each(t, function(k, v) {
					 var title=t[k].data
					 var optionData=[];
					 var value=[]
					 $.each(t[k].data, function(k1, v1) {
						v1.itemStyle = {
							normal: {
								color: v1.color,
							 }
						 }
					 	 value.push(v1);
					 });
					 console.log(value);
					 var option= {
					     tooltip: {
					         trigger: 'axis',
					         axisPointer: {            // 坐标轴指示器，坐标轴触发有效
					             type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
					         }
					     },
					     title:{
					            text:v.table_name
					        },
					     legend: {
					         data: v.dept_name,
					         orient: 'vertical',  //垂直显示
				             y: 'center',    //延Y轴居中
				             x: 'right' //居右显示
					     },
					     xAxis: [
					         {
					             type: 'category',
					             data: v.title
					         }
					     ],			        
					     yAxis: [],
					     series: value
					 };
					 echarts.init($(".test")[k]).setOption(option);
				});
			}	
		}
	},'json');
}

function exportData(){	
	var data=$("form").serialize();
	if(data.indexOf("name") == -1){
		window.parent.topMessagePrompt('error','请选择成果人');
		return;
	}
	if(data.indexOf("check") == -1){
		window.parent.topMessagePrompt('error','没有选择导出的数据');
		return;
	}
	var load_alert=loadAlert('数据统计中...');
	$.ajax({
		url: '	Admin/Results/schoolDataExport',
		type: 'POST',
		dataType: 'json',
		data: data,
		success:function(data) {
			if(data.status){
				closeAlert(load_alert);
				window.location.href=data.v;
			}
		}
	})
}